package com.metal.meta.exception;

import java.util.Objects;

/**
 * @author wys
 * @date 2018/03/20
 * @description
 */
public enum ResponseCode {

    NOT_SUPPORT_TYPE(140012, "生成sql时遇到不支持的类型"),

    // mongo
    DUPLICATE_KEY_ERROR(140011, "插入MongoDB的数据违反唯一约束"),

    ONE_LINE_EXPECTED(140010, "预计一条数据"),

    // 通用错误码
    CODE_SUCCESS(100000, "处理成功"),
    CODE_ERROR(999999, "处理失败"),
    CODE_EXTERNAL_ERROR(444444, "外部系统处理失败");




    private Integer code;
    private String msg;
    private boolean success;
    private Object data;

    ResponseCode(int code, String msg, boolean success) {
        this.setCode(code);
        this.setMsg(msg);
        this.setSuccess(success);
    }

    ResponseCode(int code, String msg) {
        this.setCode(code);
        this.setMsg(msg);
        this.setSuccess(true);
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public boolean isSuccess() {
        success = (Objects.equals(code, CODE_SUCCESS.getCode()));
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public static ResponseCode buildParamsCode(String field) {
        ResponseCode code = ResponseCode.CODE_ERROR;
        code.setMsg(field);
        return code;
    }

    @Override
    public String toString() {
        return "ResponseCode{" +
                "code=" + code +
                ", msg='" + msg + '\'' +
                ", success=" + success +
                ", data=" + data +
                '}';
    }
}
